package com.youhome.wechat.controller;

import com.github.pagehelper.PageInfo;
import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.common.response.ResultPage;
import com.youhome.service.model.HotCityHouseCount;
import com.youhome.service.model.HouseMap;
import com.youhome.service.model.HouseMapShort;
import com.youhome.service.model.ListHubList;
import com.youhome.service.parameter.ParamHouseMapSearch;
import com.youhome.service.parameter.ParamHouseSearch;
import com.youhome.service.service.*;
import com.youhome.wechat.dto.DTOMapSearch;
import com.youhome.wechat.dto.DTOSearch;
import io.swagger.annotations.*;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by chen365 on 2018/05/09.
 */
@RestController
@RequestMapping(value = "/house/")
@Api(tags = "房源")
public class HouseController {

    @Autowired
    private ListHubService listHubService;
    @Autowired
    private ListHubPhotoService listHubPhotoService;
    @Autowired
    private SolrService solrService;
    @Autowired
    private HotCityService hotCityService;
    @Autowired
    private HouseVideoService houseVideoService;
    @Autowired
    private HousePropertySubTypeService housePropertySubTypeService;

    @ApiOperation(value = "房源类型", notes = "房源类型", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/subType")
    public Object subType() {
        return new Result<>(ResultCode.SUCCESS, housePropertySubTypeService.selectByAll());
    }

    @ApiOperation(value = "房源详情", notes = "房源详情", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/{houseId}/details")
    public Object details(@PathVariable("houseId") String houseId) {
        return new Result<>(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "房源特征", notes = "房源特征", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/{houseId}/characteristics")
    public Object characteristics(@PathVariable("houseId") String houseId) throws CustomException {
        return new Result(ResultCode.SUCCESS, listHubService.selectCharacteristics(houseId));
    }

    @ApiOperation(value = "房源基本信息", notes = "房源基本信息", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/{houseId}/basicInfo")
    public Object basicInfo(@PathVariable("houseId") String houseId) throws CustomException {
        return new Result(ResultCode.SUCCESS, listHubService.selectBasicInfo(houseId));
    }

    @ApiOperation(value = "房源描述", notes = "房源描述", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/{houseId}/description")
    public Object description(@PathVariable("houseId") String houseId) throws CustomException {
        return new Result(ResultCode.SUCCESS, listHubService.selectListingDescription(houseId));
    }

    @ApiOperation(value = "房源图片", notes = "房源图片", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/{houseId}/photos")
    public Object photo(@PathVariable("houseId") String houseId) {
        return new Result(ResultCode.SUCCESS, listHubPhotoService.selectPhotoByHouseId(houseId));
    }

    @ApiOperation(value = "房源搜索", notes = "房源搜索", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping("/search")
    public Object search(@ApiParam @Validated DTOSearch dtoSearch) throws CustomException {
        ModelMapper modelMapper = new ModelMapper();
        ParamHouseSearch map = modelMapper.map(dtoSearch, ParamHouseSearch.class);
        PageInfo<ListHubList> listHubListPageInfo = solrService.searchByDTOSearch(map);
        return new ResultPage(ResultCode.SUCCESS, listHubListPageInfo);
    }

    @ApiOperation(value = "房源地图搜索", notes = "房源地图搜索", response = HouseMapShort.class, responseContainer = "List", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping("/search/map/short")
    public Object searchMapShort(@ApiParam @Validated DTOMapSearch dtoMapSearch) throws CustomException {
        ModelMapper modelMapper = new ModelMapper();
        return new Result(ResultCode.SUCCESS, solrService.searchMapShortByBean(modelMapper.map(dtoMapSearch, ParamHouseMapSearch.class)));
    }

    @ApiOperation(value = "房源地图搜索", notes = "房源地图搜索", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "action", value = "查询方法(zip,address,city)", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "keyword", value = "查询条件", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "type", value = "房屋类型", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "beds", value = "卧室数量", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "baths", value = "卫浴数量", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "year", value = "房龄", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "minSize", value = "最小面积", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "maxSize", value = "最大面积", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "minPrice", value = "最小价格", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "maxPrice", value = "最大价格", required = false, paramType = "query"),

            @ApiImplicitParam(dataType = "Double", name = "minX", value = "minX", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "Double", name = "maxX", value = "maxX", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "Double", name = "minY", value = "minY", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "Double", name = "maxY", value = "maxY", required = false, paramType = "query"),

            @ApiImplicitParam(dataType = "String", name = "filed", value = "排序字段(bedroomCount：卧室数量;bathroomCount：浴数量;yearBuilt：房龄;livingArea：面积;listPrice:价格)", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "direction", value = "排序方向(asc：升序  desc：降序)", required = false, paramType = "query")

    })
    @GetMapping("/search/map")
    public Object searchMap(
            @RequestParam(value = "action", defaultValue = "") String action,
            @RequestParam(value = "keyword", defaultValue = "") String keyword,
            @RequestParam(value = "type", defaultValue = "") String type,
            @RequestParam(value = "beds", defaultValue = "") Integer beds,
            @RequestParam(value = "baths", defaultValue = "") Integer baths,
            @RequestParam(value = "year", defaultValue = "") Integer year,
            @RequestParam(value = "minSize", defaultValue = "") Double minSize,
            @RequestParam(value = "maxSize", defaultValue = "") Double maxSize,
            @RequestParam(value = "minPrice", defaultValue = "") Double minPrice,
            @RequestParam(value = "maxPrice", defaultValue = "") Double maxPrice,
            @RequestParam(value = "filed", defaultValue = "") String filed,
            @RequestParam(value = "direction", defaultValue = "asc") String direction,
            @RequestParam(value = "minX", defaultValue = "") Double minX,
            @RequestParam(value = "maxX", defaultValue = "") Double maxX,
            @RequestParam(value = "minY", defaultValue = "") Double minY,
            @RequestParam(value = "maxY", defaultValue = "") Double maxY
    ) {
        try {
            long btime = System.currentTimeMillis();
            if (!StringUtils.isEmpty(action) && !StringUtils.isEmpty(keyword)) {
                if (action.equalsIgnoreCase("zip")) {
                    if (keyword.length() != 5) {
                        return new ResultPage(ResultCode.HOUSE_ILLEGALITY_ZIP_CODE);
                    }
                } else if (action.equalsIgnoreCase("address")) {
                    if (keyword.length() < 3) {
                        return new ResultPage(ResultCode.HOUSE_ADDRESS_LENGTH_SHORTAGE);
                    }
                } else if (action.equalsIgnoreCase("city")) {
                    if (keyword.length() < 3) {
                        return new ResultPage(ResultCode.HOUSE_CITY_LENGTH_SHORTAGE);
                    }
                } else {
                    return new ResultPage(ResultCode.UNSUPPORTED_SEARCH_TYPE);
                }
            }

            List<HouseMap> list = solrService.searchMapByBean(action, keyword, type, beds, baths, year, minSize, maxSize, minPrice, maxPrice, filed, direction, null, null, minX, maxX, minY, maxY, null);

            long etime = System.currentTimeMillis();
            System.err.println("执行时间：" + (etime - btime) + "ms");

            if (list.size() > 0) {
                return new Result(ResultCode.SUCCESS, list);
            } else {
                return new Result(ResultCode.NO_DATA);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAIL);
        }
    }

    @ApiOperation(value = "搜索联想", notes = "搜索联想", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "keyword", value = "keyword", required = true, paramType = "path")
    })
    @GetMapping("/associate/{keyword}")
    public Object associate(@PathVariable(value = "keyword") String keyword) throws CustomException {
        return new Result(ResultCode.SUCCESS, solrService.associateSearch(keyword));
    }

    @ApiOperation(value = "十大热门城市房源数量", notes = "十大热门城市房源数量", response = HotCityHouseCount.class, responseContainer = "List", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/hot-city/house-count")
    public Object hotCityCount() {
        return new Result<>(ResultCode.SUCCESS, hotCityService.selectHotCityHouseCount());
    }


    @ApiOperation(value = "视频", notes = "视频", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    @GetMapping("/videos/{houseId}")
    public Object videos(@PathVariable(value = "houseId") String houseId) throws CustomException {
        return new Result<>(ResultCode.SUCCESS, houseVideoService.selectVideoByHouseId(houseId));
    }

}

